// $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.alwaysOverridetoString.alwaysOverrideToString, spellCheckIdentifiers, spellCheckStringLiterals, spellCheckComments
/**
 * Package logging
 */
package teamrocket.logging; // $codepro.audit.disable packagePrefixNamingConvention

	import java.text.SimpleDateFormat;
	import java.util.Date;
	import java.util.logging.Formatter;
	import java.util.logging.Handler;
	import java.util.logging.Level;
	import java.util.logging.LogRecord;

/**
 * This class a log formatter using html coding.
 * @author James Zhang
 * @version 1.0
 */
public class THTMLFormatter extends Formatter{
	/**
	 * Method format.
	 * @param rec LogRecord
	 * @return String
	 */
	public String format(LogRecord rec) {
		final StringBuffer buf = new StringBuffer(1000); // $codepro.audit.disable numericLiterals
		buf.append("<tr>");
		buf.append("<td>");
		if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
			buf.append("<b>");
			buf.append(rec.getLevel());
			buf.append("</b>");
		} else {
			buf.append(rec.getLevel());
		}
		buf.append("</td>");
		buf.append("<td>");
		buf.append(calcDate(rec.getMillis()));
		buf.append("</td>");
		buf.append("<td>");
		buf.append(formatMessage(rec));
		buf.append("</td>");
		buf.append("</tr>");
		buf.append('\n');
		return buf.toString();
	}

	/**
	 * Method calcDate.
	 * @param millisecs long
	 * @return String
	 */
	private String calcDate(long millisecs) {
		final SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
		final Date resultdate = new Date(millisecs);
		return date_format.format(resultdate);
	}

	/**
	 * Method getHead.
	 * @param h Handler
	 * @return String
	 */
	public String getHead(Handler h) {
		return "<HTML>\n<HEAD>\n" + (new Date()) + "\n</HEAD>\n<BODY>\n<PRE>\n"
				+ "<table border>\n  "
				+ "<tr><th>Level</th><th>Time</th><th>Log Message</th></tr>\n";
	}

	/**
	 * Method getTail.
	 * @param h Handler
	 * @return String
	 */
	public String getTail(Handler h) {
		return "</table>\n  </PRE></BODY>\n</HTML>\n";
	}
}

